//
//  AppDelegate.m
//  Knight_Tour
//
//  Created by Bruce_Lin on 15/5/13.
//  Copyright (c) 2015年 TouchOne. All rights reserved.
//

#import "AppDelegate.h"
#import "LHTabBarController.h"
#import "LHGuideViewController.h"
#import <BaiduMapAPI/BMapKit.h>
#import "LHTabBarController.h"
#import "UMSocial.h"
#import "AFNetworkReachabilityManager.h"

@interface AppDelegate ()
@property(nonatomic ,retain)BMKMapManager *mapManager;
@end

@implementation AppDelegate
- (void)dealloc
{
    [_mapManager release];
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    }
    else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
        if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Not Reachable"]) {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络没有连接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WWAN"]){
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络为WLAN网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }else if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WiFi"]){
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前网络为wifi网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

    }];
    
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    self.mapManager = [[[BMKMapManager alloc]init]autorelease];
    
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:@"wK11O1pQmoM6Ik7YikrbkYU7"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //判断是否为第一次登陆
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        LHGuideViewController *vc = [[LHGuideViewController alloc]init];
        self.window.rootViewController = vc;
        [vc release];
        [_window release];
    }
    else
    {
        LHTabBarController *tbVC = [[LHTabBarController alloc]init];
        self.window.rootViewController = tbVC;
        [tbVC release];
        [_window release];
    }
    
    // 分享功能APPKey
    [UMSocialData setAppKey:@"55603d4f67e58e768200361e"];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    [BMKMapView willBackGround];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [BMKMapView didForeGround];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
